import nltk
class TextNode:
    instFact = None
    
    def __init__(self, reqID=None, textStr=None, classList = None, preproc = None):
        self.reqID = reqID
        self.textStr = textStr
        self.classFreqDist = nltk.FreqDist()
        if classList:
            for clz in classList: self.classFreqDist.inc(clz)           
        self.preprocessor = preproc
        self.wordsFreqDist = nltk.FreqDist()
        self.stoppedWordFreqDist = nltk.FreqDist()
        self.invalidWordFreqDist = nltk.FreqDist()
        self.conceptIdFreqDist = nltk.FreqDist()
        
    def integrateTokens(self):
        if self.conceptIdFreqDist:
            kvs = self.conceptIdFreqDist.items()
            for (k,v) in kvs:
                self.wordsFreqDist.inc(k,v)
            self.conceptIdFreqDist = None
        return
            
        
    def __str__(self):
        return "%d,%s,%s"%(self.reqID,str(self.textStr),str(self.classFreqDist))
        
    def getReqID(self):
        return self.reqID
        
    def getFileStr(self):
        return self.textStr
    
    def getClassFreqDist(self):
        return self.classFreqDist
    
    def tokenizeTextString(self, separateSection, needConceptId, replaceStrWithId, keepIdOnly, nForGram):
        self.wordsFreqDist = self.preprocessor.tokenize(self, separateSection, needConceptId, replaceStrWithId, keepIdOnly, gram_N = nForGram)
        ##!!!!!!!!!!<TRIAL>
        self.textStr = ''
        ##!!!!!!!!!!</TRIAL>
        return
    
    def removeStopwords(self):
        (self.wordsFreqDist, self.stoppedWordFreqDist) = self.preprocessor.removeStopwords(self.wordsFreqDist)
        return
    
    def lexicalVerify(self):
        (self.wordsFreqDist, self.invalidWordFreqDist) = self.preprocessor.lexicalVerify(self.wordsFreqDist)
        return
    
    def stem(self, scale):
        self.preprocessor.stem(self.wordsFreqDist, scale)
        return
    
    def checkInClasses(self, globalClassFreqDist):
        """
        Record class-histogram from this instance into 'globalClassFreqDist'  
        """
        for classValue in self.classFreqDist.keys():
            globalClassFreqDist.inc(classValue, self.classFreqDist[classValue])
        return
    
    def checkInNodeOfCode(self, quantityOfNodeOfCodeFreqDist, codeList):
        if codeList == None:
            for key in self.classFreqDist.keys():
                quantityOfNodeOfCodeFreqDist.inc(key)
        else:
            for key in self.classFreqDist.keys():
                if key in codeList:
                    quantityOfNodeOfCodeFreqDist.inc(key)
        return



class classDistNode:
    def __init__(self, diagCode, frequency):
        self.diagCode = diagCode
        self.frequency = frequency
    
class repNodeClassHistoNode:
    def __init__(self, requestId, diagCodeOccurs, diagCodeType):
        self.requestId = requestId
        self.diagCodeOccurs = diagCodeOccurs
        self.diagCodeType = diagCodeType
    
class repClassDistNode:
    def __init__(self, requestId, diagCode, amount):
        self.requestId = requestId
        self.diagCode = diagCode
        self.amount = amount
    
class DiagCodeDistNode:
    def __init__(self, dc, amount):
        self.dc = dc
        self.amount = amount
